home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / general / termios.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  5KB  |  215 lines

  1. /*-
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)termios.c    5.9 (Berkeley) 5/20/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define KERNEL
  39. #include "ixemul.h"
  40. #include "kprintf.h"
  41.  
  42. #include <sys/types.h>
  43. #include <sys/errno.h>
  44. #undef KERNEL
  45. #include <sys/ioctl.h>
  46. #include <sys/tty.h>
  47. #define KERNEL    /* XXX - FREAD and FWRITE was ifdef'd KERNEL*/
  48. #include <sys/fcntl.h>
  49. #undef KERNEL
  50. #include <termios.h>
  51. #include <stdio.h>
  52. #include <unistd.h>
  53.  
  54. int tcgetattr(int fd, struct termios *t)
  55. {
  56.     return(ioctl(fd, TIOCGETA, t));
  57. }
  58.  
  59. int tcsetattr(int fd, int opt, const struct termios *t)
  60. {
  61.     struct termios localterm;
  62.  
  63.     if (opt & TCSASOFT) {
  64.         localterm = *t;
  65.         localterm.c_cflag |= CIGNORE;
  66.         t = &localterm;
  67.         opt &= ~TCSASOFT;
  68.     }
  69.     if (opt == TCSANOW)
  70.         return (ioctl(fd, TIOCSETA, t));
  71.     else if (opt == TCSADRAIN)
  72.         return (ioctl(fd, TIOCSETAW, t));
  73.     return (ioctl(fd, TIOCSETAF, t));
  74. }
  75.  
  76. int tcsetpgrp(int fd, pid_t pgrp)
  77. {
  78.     int s;
  79.  
  80.     s = pgrp;
  81.     return(ioctl(fd, TIOCSPGRP, &s));
  82. }
  83.  
  84. pid_t tcgetpgrp(int fd)
  85. {
  86.     int s;
  87.  
  88.     if (ioctl(fd, TIOCGPGRP, &s) < 0)
  89.         return((pid_t)-1);
  90.  
  91.     return((pid_t)s);
  92. }
  93.  
  94. speed_t cfgetospeed(const struct termios *t)
  95. {
  96.     return(t->c_ospeed);
  97. }
  98.  
  99. speed_t cfgetispeed(const struct termios *t)
  100. {
  101.     return(t->c_ispeed);
  102. }
  103.  
  104. int cfsetospeed(struct termios *t, speed_t speed)
  105. {
  106.     t->c_ospeed = speed;
  107.  
  108.     return (0);
  109. }
  110.  
  111. int cfsetispeed(struct termios *t, speed_t speed)
  112. {
  113.     t->c_ispeed = speed;
  114.  
  115.     return (0);
  116. }
  117.  
  118. void cfsetspeed(struct termios *t, speed_t speed)
  119. {
  120.     t->c_ispeed = t->c_ospeed = speed;
  121. }
  122.  
  123. /*
  124.  * Make a pre-existing termios structure into "raw" mode:
  125.  * character-at-a-time mode with no characters interpreted,
  126.  * 8-bit data path.
  127.  */
  128. void cfmakeraw(struct termios *t)
  129. {
  130.     t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  131.     t->c_oflag &= ~OPOST;
  132.     t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  133.     t->c_cflag &= ~(CSIZE|PARENB);
  134.     t->c_cflag |= CS8;
  135.     /* set MIN/TIME */
  136. }
  137.  
  138. int tcsendbreak(int fd, int len)
  139. {
  140.     struct timeval sleepytime;
  141.  
  142.     sleepytime.tv_sec = 0;
  143.     sleepytime.tv_usec = 400000;
  144.     if (ioctl(fd, TIOCSBRK, 0) == -1)
  145.         return (-1);
  146.     select(0, 0, 0, 0, &sleepytime);
  147.     if (ioctl(fd, TIOCCBRK, 0) == -1)
  148.         return (-1);
  149.  
  150.     return (0);
  151. }
  152.  
  153. int tcdrain(int fd)
  154. {
  155.     if (ioctl(fd, TIOCDRAIN, 0) == -1)
  156.         return (-1);
  157.  
  158.     return (0);
  159. }
  160.  
  161. int tcflush(int fd, int which)
  162. {
  163.     int com;
  164.  
  165.     switch (which) {
  166.     case TCIFLUSH:
  167.         com = FREAD;
  168.         break;
  169.     case TCOFLUSH:
  170.         com = FWRITE;
  171.         break;
  172.     case TCIOFLUSH:
  173.         com = FREAD | FWRITE;
  174.         break;
  175.     default:
  176.         errno = EINVAL;
  177.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  178.         return (-1);
  179.     }
  180.     if (ioctl(fd, TIOCFLUSH, &com) == -1)
  181.         return (-1);
  182.  
  183.     return (0);
  184. }
  185.  
  186. int tcflow(int fd, int action)
  187. {
  188.     switch (action) {
  189.     case TCOOFF:
  190.         return (ioctl(fd, TIOCSTOP, 0));
  191.         break;
  192.     case TCOON:
  193.         return (ioctl(fd, TIOCSTART, 0));
  194.         break;
  195.     case TCIOFF:
  196.     case TCION: {        /* these posix functions are STUPID */
  197.         struct termios term;
  198.         unsigned char c;
  199.  
  200.         if (tcgetattr(fd, &term) == -1)
  201.             return (-1);
  202.         c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
  203.         if (c != _POSIX_VDISABLE && write(fd, &c, 1) == -1)
  204.             return (-1);
  205.         break;
  206.     }
  207.     default:
  208.         errno = EINVAL;
  209.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  210.         return (-1);
  211.     }
  212.  
  213.     return (0);
  214. }
  215.